package com.clkj.cldisabledmanclient.mvp.satisfactionresearch;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.clkj.cldisabledmanclient.R;
import com.clkj.cldisabledmanclient.adapter.jaylv.JayLvBaseAdapter;
import com.clkj.cldisabledmanclient.adapter.jaylv.ViewHolder;
import com.clkj.cldisabledmanclient.app.CustomApplication;
import com.clkj.cldisabledmanclient.base.BaseActivity;
import com.clkj.cldisabledmanclient.bean.MyRecoverPlanBean;
import com.clkj.cldisabledmanclient.bean.MyRecoverTrainBean;
import com.clkj.cldisabledmanclient.bean.SatisfationResearchBean;
import com.clkj.cldisabledmanclient.constant.Config;
import com.clkj.cldisabledmanclient.constant.ShareKey;
import com.clkj.cldisabledmanclient.constant.TipStr;
import com.clkj.cldisabledmanclient.mvp.myrecovertrain.MyRecoverTrainActivity;
import com.clkj.cldisabledmanclient.mvp.myrecovertrain.MyRecoverTrainDetailActivity;
import com.clkj.cldisabledmanclient.util.CommonUtil;
import com.clkj.cldisabledmanclient.util.ToastUtil;
import com.clkj.cldisabledmanclient.util.scheduler.SchedulerProvider;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

public class SatisfationResearchActivity extends BaseActivity implements SatisfationResearchContract.View, PullToRefreshBase.OnRefreshListener2 {

    private PullToRefreshListView lv;
    private RelativeLayout rlEmpty;
    private TextView tvTipEmpty;

    private ProgressDialog progressDialog;
    private List<SatisfationResearchBean> researchBeanList = new ArrayList<>();
    private JayLvBaseAdapter<SatisfationResearchBean> mAdapter;

    private SatisfationResearchContract.Presenter mPresenter;
    /**
     * 刷新标识
     */
    boolean isRefresh = false;
    /**
     * 是否可以加载更多标识
     */
    boolean canLoad = false;
    private Integer mPageIndex = 1;

    private String userName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_satisfation_research);
        initTitle(null, null, "满意度调查", true, null);
        initView();
        initData();
        assignView();

        getSatisfationResearchList();
    }

    @Override
    public void initData() {

        userName = (String) CommonUtil.getShareValue(ShareKey.USER_IDCARD, "");
        mPresenter = new SatisfationResearchPresenter(this, SchedulerProvider.getInstance(), CustomApplication.getInstance().getHttpService());
    }

    @Override
    public void assignView() {

        mAdapter = new JayLvBaseAdapter<SatisfationResearchBean>(this, researchBeanList, R.layout.item_satisfation_research) {
            @Override
            public void convert(ViewHolder helper, int position, SatisfationResearchBean item) {

                helper.setText(R.id.tv_searchTitle, item.getQname());
                helper.setText(R.id.tv_duixiang, item.getqQrespondent());
            }
        };
        lv.setAdapter(mAdapter);

        lv.setOnRefreshListener(this);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent toDetail = new Intent(SatisfationResearchActivity.this, ResearchDetailActivity.class);
                toDetail.putExtra("qid", researchBeanList.get(position - 1).getqId());
                startActivityForResult(toDetail, 0x111);
            }
        });
    }

    @Override
    public void initView() {

        lv = (PullToRefreshListView) findViewById(R.id.lv);
        rlEmpty = (RelativeLayout) findViewById(R.id.rl_empty);
        tvTipEmpty = (TextView) findViewById(R.id.tv_tip_empty);
    }

    @Override
    public void getSatisfationResearchList() {

        mPresenter.getSatisfationResearchList(userName, mPageIndex.toString(), Config.DEFAULT_PAGE_SIZE.toString());
    }

    @Override
    public void getSatisfationResearchListSuccess(List<SatisfationResearchBean> satisfationResearchBeans) {

        lv.onRefreshComplete();

        if (isRefresh) {
            researchBeanList.clear();
            isRefresh = false;
        }

        if (satisfationResearchBeans.size() > 0) {

            if (satisfationResearchBeans.size() >= Config.DEFAULT_PAGE_SIZE) {
                lv.setMode(PullToRefreshBase.Mode.BOTH);
                canLoad = true;
            } else {
                canLoad = false;
                lv.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
            }
            researchBeanList.addAll(satisfationResearchBeans);

        } else {
            canLoad = false;
            lv.setMode(PullToRefreshBase.Mode.PULL_FROM_START);

            if (researchBeanList.size() == 0) {
                lv.setEmptyView(rlEmpty);
                tvTipEmpty.setText("未查询到您的康复计划数据");
            } else {
                ToastUtil.showShort(this, TipStr.TIP_NOT_MORE_DATAS);
            }
        }

        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void getSatisfationResearchListError(String tip) {
        lv.onRefreshComplete();
        if (isRefresh) {
            researchBeanList.clear();
            isRefresh = false;
        }
        ToastUtil.showShort(this, tip);
        canLoad = false;
        lv.setMode(PullToRefreshBase.Mode.PULL_FROM_START);

        if (researchBeanList.size() == 0) {
            lv.setEmptyView(rlEmpty);
        }
    }

    @Override
    public void setPresenter(SatisfationResearchContract.Presenter presenter) {

        this.mPresenter = presenter;
    }

    @Override
    public void showLoading() {
        progressDialog = new ProgressDialog(this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setIndeterminate(false);
        progressDialog.setCancelable(true);
        //设置点击进度对话框外的区域对话框不消失
        progressDialog.setCanceledOnTouchOutside(false);
        progressDialog.setMessage("正在加载...");
        progressDialog.show();
    }

    @Override
    public void dismissLoading() {
        if (progressDialog != null) {
            progressDialog.dismiss();
            progressDialog = null;
        }
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase pullToRefreshBase) {

        isRefresh = true;
        mPageIndex = 1;
        getSatisfationResearchList();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase pullToRefreshBase) {
        mPageIndex++;
        getSatisfationResearchList();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0x111 && resultCode == RESULT_OK) {
            mPageIndex = 1;
            getSatisfationResearchList();
        }
    }
}
